package myjava.d_time;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

/**
 * @Auther: cty
 * @Date: 2019/11/6 下午9:25
 * @Description: 可视化日历程序
 * @version: 1.0
 */
public class D_TestCalendar2 {
    public static void main(String[] args) throws ParseException {
        System.out.print("请输入日期（格式：2020-10-10）：");
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();  //获取键盘输入

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");  //设置字符格式
        Date date = df.parse(str);  //将字符串转化为时间对象
        Calendar c = new GregorianCalendar();  //新建一个日期对象
        c.setTime(date);  //将时间对象转化为日期对象

        int day = c.get(Calendar.DATE);  //保存日信息
        c.set(Calendar.DATE,1);

        System.out.println("日\t一\t二\t三\t四\t五\t六");

        int week = c.get(Calendar.DAY_OF_WEEK)-1;
        int week2 = (week==0)?7:week;
        for(int i=1;i<week2;i++){  //每个月的第一天是周几就在前面打印几个制表符
            System.out.print("\t");
        }

        int maxDayOfMonth = c.getActualMaximum(Calendar.DATE);
        for(int i=1;i<=maxDayOfMonth;i++){
            if(c.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){  //周日换行
                System.out.println();
            }

            if(day==c.get(Calendar.DATE)){
                System.out.print(c.get(Calendar.DATE)+"*"+"\t");
            }
            else{
                System.out.print(c.get(Calendar.DATE)+"\t");
            }

            c.add(Calendar.DATE,1);
        }
    }
}
